package com.picture.wallpaper.manager.OBS;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import shade.okhttp3.Call;
import shade.okhttp3.OkHttpClient;
import shade.okhttp3.Request;
import shade.okhttp3.Response;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class ObsUtils {
    @Autowired
    ObsClient obsClient;
//    输入对象标识返回对象存储临时连接
    public List<ObsBucket> GetObjectList() throws ObsException{
            // 列举桶
           try{ ListBucketsRequest request = new ListBucketsRequest();
            request.setQueryLocation(true);
            List<ObsBucket> buckets = obsClient.listBuckets(request);
            return buckets;}
           catch (ObsException e) {
               log.warn("http状态码:"+e.getResponseCode()+",服务端错误码:"+e.getErrorCode()+",详细错误信息:"+e.getErrorMessage()
                       +"Request ID:"+e.getErrorRequestId()+"Host ID:"+e.getErrorHostId());
               return null;
           }
           catch (Exception e) {
               // 其他异常信息打印
               log.error("error",e);
               return null;
           }
    }

    //    ObjectKey文件名（带后缀），expireSeconds链接过期时间，BucketName桶名字
     public String GetObjectUrl(long expireSeconds,String BucketName,String ObjectKey) throws ObsException{
        try {
            TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
            request.setBucketName(BucketName);
            request.setObjectKey(ObjectKey);
            TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
            return response.getSignedUrl();
        }
        catch (ObsException e) {
            log.warn("http状态码:"+e.getResponseCode()+",服务端错误码:"+e.getErrorCode()+",详细错误信息:"+e.getErrorMessage()
            +"Request ID:"+e.getErrorRequestId()+"Host ID:"+e.getErrorHostId());
            return null;
        }
        catch (Exception e) {
            // 其他异常信息打印
            log.error("error",e);
            return null;
        }
    }

//    ObjectKey文件名（带后缀），FileUrl文件路径（带文件名和文件名的后缀），BucketName桶名字
    public PutObjectResult PutObject(String BucketName,String ObjectKey,String FileUrl){
        try {
            // 文件上传
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(BucketName);
            request.setObjectKey(ObjectKey);
            request.setFile(new File(FileUrl));
            PutObjectResult putObjectResult=obsClient.putObject(request);
            return putObjectResult;
        }
        catch (ObsException e) {
            log.warn("http状态码:"+e.getResponseCode()+",服务端错误码:"+e.getErrorCode()+",详细错误信息:"+e.getErrorMessage()
                    +"Request ID:"+e.getErrorRequestId()+"Host ID:"+e.getErrorHostId());
            return null;
        }
        catch (Exception e) {
            // 其他异常信息打印
            log.error("error",e);
            return null;
        }
    }
    //列举桶内的对象
    public ObjectListing ListObject(String bucketName){

        try {
            // 简单列举
            return obsClient.listObjects(bucketName);

        }  catch (ObsException e) {
            log.warn("http状态码:"+e.getResponseCode()+",服务端错误码:"+e.getErrorCode()+",详细错误信息:"+e.getErrorMessage()
                    +"Request ID:"+e.getErrorRequestId()+"Host ID:"+e.getErrorHostId());
            return null;
        }
        catch (Exception e) {
            // 其他异常信息打印
            log.error("error",e);
            return null;
        }
    }
}



